#!/usr/bin/env bash
. ./scripts/devcontainer/_assert-in-container "$0" "$@"

set -euo pipefail

# TODO remove this and stop-second-instance
# (temporary until we have a 'real' server standing.)

# This script runs a second Darklang instance for testing sync
# Instance 1 (default): port 11001, database data.db
# Instance 2 (this one): port 11003, database data-instance2.db

INSTANCE_NAME="instance2"
INSTANCE_PORT=11003
INSTANCE_DB="data-instance2.db"

grey="\033[1;30m"
green="\033[0;32m"
reset="\033[0m"

LOGS="${DARK_CONFIG_RUNDIR}/logs"
INSTANCE_LOG="$LOGS/bwdserver-${INSTANCE_NAME}.log"

# Check if database exists, if not, copy from main instance
DB_PATH="${DARK_CONFIG_RUNDIR}/${INSTANCE_DB}"
MAIN_DB_PATH="${DARK_CONFIG_RUNDIR}/data.db"


echo -e "${green}Creating new database for ${INSTANCE_NAME}${reset}"
rm -f $DB_PATH $DB_PATH-wal $DB_PATH-shm
# Use sqlite3 backup to create consistent copy
sqlite3 "${MAIN_DB_PATH}" ".backup '${DB_PATH}'"
echo -e "${green}Database created: ${DB_PATH}${reset}"

# Find the BwdServer executable
PUBLISHED=false
if [[ "$PUBLISHED" == "true" ]]; then
  BWDSERVER_EXE="backend/Build/out/BwdServer/Release/net8.0/linux-x64/publish/BwdServer"
else
  BWDSERVER_EXE="backend/Build/out/BwdServer/Debug/net8.0/BwdServer"
fi

# Wait for build if needed
echo "Waiting for BwdServer build..."
for ((i=1;i<=1000;i++)); do
  if [[ ! -f "${BWDSERVER_EXE}" ]]; then
    sleep 0.01
  else
    break
  fi
done

if [[ ! -f "${BWDSERVER_EXE}" ]]; then
  echo "Error: BwdServer executable not found at ${BWDSERVER_EXE}"
  exit 1
fi

# Stop any existing instance2 process
echo "Stopping any existing ${INSTANCE_NAME} processes..."
pkill -f "DARK_CONFIG_DB_NAME=${INSTANCE_DB}" || true
sleep 1


# Record initial sync to mark instances as in-sync at this point
# This prevents trying to re-sync all historical ops
echo "Recording initial sync state..."

# Get actual instance2 UUID from main database
SECOND_INSTANCE_ID=$(sqlite3 "${MAIN_DB_PATH}" "SELECT id FROM instances WHERE name = 'instance2'")

# Clean up instance2's instances table and add instance1 with a known UUID
LOCAL_INSTANCE_ID="00000000-0000-0000-0000-000000000001"
sqlite3 "${DB_PATH}" "DELETE FROM instances WHERE name = 'instance2';"
sqlite3 "${DB_PATH}" "INSERT INTO instances (id, name, url) VALUES ('${LOCAL_INSTANCE_ID}', 'instance1', 'http://dark-packages.dlio.localhost:11001');"

# Record bootstrap sync records to prevent re-syncing all historical ops.
# Since we just copied the database, both instances are already in sync at this moment.
sqlite3 "${MAIN_DB_PATH}" "INSERT OR REPLACE INTO syncs (id, instance_id, synced_at, ops_pushed, ops_fetched) VALUES ('00000000-0000-0000-0000-000000000000', '${SECOND_INSTANCE_ID}', strftime('%Y-%m-%dT%H:%M:%SZ', 'now'), 0, 0);"
sqlite3 "${DB_PATH}" "INSERT OR REPLACE INTO syncs (id, instance_id, synced_at, ops_pushed, ops_fetched) VALUES ('00000000-0000-0000-0000-000000000000', '${LOCAL_INSTANCE_ID}', strftime('%Y-%m-%dT%H:%M:%SZ', 'now'), 0, 0);"

echo "Initial sync recorded"

# Start the second instance
echo -e "${green}Starting ${INSTANCE_NAME} on port ${INSTANCE_PORT}${reset}"
echo -e "${grey}Database: ${DB_PATH}${reset}"
echo -e "${grey}Log file: ${INSTANCE_LOG}${reset}"
echo ""

DARK_CONFIG_DB_NAME="${INSTANCE_DB}" \
DARK_CONFIG_BWDSERVER_BACKEND_PORT="${INSTANCE_PORT}" \
DARK_CONFIG_BWDSERVER_KUBERNETES_PORT="11004" \
"${BWDSERVER_EXE}" > "${INSTANCE_LOG}" 2>&1 &

PID=$!
echo -e "${green}Instance ${INSTANCE_NAME} started (PID: ${PID})${reset}"
echo ""
echo "Access it at: http://dark-packages.dlio.localhost:${INSTANCE_PORT}/stats"
echo ""
echo "View logs:"
echo "  tail -f ${INSTANCE_LOG}"
